package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class EnterprisePopWindow extends BasePopWindow {

    private MaxHeightRecyclerView recyclerView;
    private MaxHeightRecyclerView anymore_list;
    private LinearLayout ll_next;
    private LinearLayout ll_input;
    private TextView tv_next;
    private TextView tv_ok;
    private TextView tv_dic;
    private EditText et_dic;
    private LinearLayout bottomLayout;
    private PickAdapter pickAdapterCity;
    private List<PickSelectEntity> cityDatas;
    private String no = "";
    private String name = "";
    private int position = -1;

    public EnterprisePopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_manage;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        anymore_list = holder.getPop().findViewById(R.id.anymore_list);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);
        ll_input = holder.getPop().findViewById(R.id.ll_input);
        ll_next = holder.getPop().findViewById(R.id.ll_next);
        tv_dic = holder.getPop().findViewById(R.id.tv_dic);
        et_dic = holder.getPop().findViewById(R.id.et_dic);
        tv_next = holder.getPop().findViewById(R.id.tv_next);
        tv_ok = holder.getPop().findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    if (no == null)
                        no = "";
                    onResultClick.result(no, name, position);
                    Log.d("城市编号", no);
                }
                dissMiss();
            }
        });
        tv_dic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    if (!et_dic.getText().toString().equals("")) {
                        onResultClick.result(et_dic.getText().toString(), et_dic.getText() + "KM", position);
                        pickAdapterCity.setDefItem(-1);
                        dismiss();
                    } else {
                        Toast.makeText(mContext, "请输入距离", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        pickAdapterCity = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        pickAdapterCity.setItemSelect(true);
        pickAdapterCity.setDefItem(0);
        anymore_list.setLayoutManager(new GridLayoutManager(mContext, 4));
        anymore_list.setAdapter(pickAdapterCity);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(pickAdapterCity);
        pickAdapterCity.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                no = pickSelectEntity.getNo();
                name = pickSelectEntity.getProjectscale();
                if (onResultClick != null) {
                    if (no == null)
                        no = "";
                    onResultClick.result(no, name, position);
                    et_dic.setText("");
                    Log.d("城市编号", no);
                }
                dissMiss();
            }
        });
    }

    public void setData(List<PickSelectEntity> cityDatas) {
        this.cityDatas = cityDatas;
        pickAdapterCity.setDatas(cityDatas);

    }

    public void setData(List<PickSelectEntity> cityDatas, boolean isInput, boolean isType) {
        this.cityDatas = cityDatas;
        pickAdapterCity.setDatas(cityDatas);
        if (isInput) {
            ll_input.setVisibility(View.VISIBLE);
        }
        if (isType) {
            anymore_list.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        } else {
            anymore_list.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
    }

    public void setData(List<PickSelectEntity> cityDatas, int position) {
        this.cityDatas = cityDatas;
        pickAdapterCity.setDatas(cityDatas);
        pickAdapterCity.setDefItem(position);
    }

    public void isVisibleBottomLayout(boolean visible) {
        ll_next.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    public void setVisibleList(boolean visible) {
        recyclerView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    public void setDefData(int position) {
        pickAdapterCity.setDefItem(position);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null)
            onDisMiss.disMiss();
    }
}
